package cn.manter.system.controller;

import cn.manter.basic.utils.AjaxResult;
import cn.manter.basic.utils.PageList;
import cn.manter.system.Service.IDeptService;
import cn.manter.system.domain.Dept;
import cn.manter.system.query.DeptQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private IDeptService deptService;
    @GetMapping("/{id}")
    public Dept loadById(@PathVariable("id") Long id){
        return deptService.loadById(id);
    }

    @GetMapping
    public List<Dept> loadAll(){
        return deptService.loadAll();
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            deptService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Dept dept){
        try {
            if(dept.getId() == null){
                deptService.save(dept);
            }else {
                deptService.update(dept);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    @PostMapping
    public PageList<Dept> queryData(@RequestBody DeptQuery deptQuery){
        PageList<Dept> data = deptService.queryData(deptQuery);
        return data;
    }

    @PatchMapping
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            deptService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

}
